# -*- coding: utf-8 -*-
# @time: 2024/6/5 14:07
# @file: price_skew
# @author: tyshixi08

from get_data.covprice_mid import *

# 获取转债价格平均数
def get_price_average(code=code_ls(), start_date = month_ls()[0].replace('-','') + '01', end_date = month_ls()[-1].replace('-','') + '01'):
    df = get_price(code, start_date, end_date, fields='close').reset_index()
    df['date'] = pd.to_datetime(df['date']).dt.strftime('%Y-%m-%d')
    df_price_average = df.groupby('date')['close'].mean().reset_index().rename(columns={'close':'price_mean'})
    return df_price_average

# 获取转债价格倾向
def get_covtend(code=code_ls(), start_date = month_ls()[0].replace('-','') + '01', end_date = month_ls()[-1].replace('-','') + '01'):
    df_median = get_price_median(code, start_date, end_date)
    df_average = get_price_average(code, start_date, end_date)
    df = pd.merge(df_median, df_average, how = 'outer', on = 'date')
    df['covtend'] = df['covprice_mid'] - df['price_mean']
    df = df[['date', 'covtend']]
    df = df.reset_index(drop=True).sort_values('date')
    return df.sort_values('date')

# 数据存档
def covtend_save():
    excel_file_path = 'covtend.csv'
    #if os.path.exists('原始数据/' + excel_file_path):
    #    df = pd.read_csv('原始数据/' + excel_file_path)
    #    return df
    #else:
    save_CSV(get_covtend(), 'get_data/原始数据/' + excel_file_path.split('.')[0])
    df = pd.read_csv('get_data/原始数据/' + excel_file_path)
    return df